<template>
  <!-- 写病历-修改病历 -->
  <Dialog :title="title" :visible="visible" :reveal="false" @close="close">
    <el-form ref="ruleForm" :model="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item
        label="顾客:"
        prop="name"
        :rules="[
          { required: true, message: '请输入活动名称', trigger: 'blur' },
        ]"
      >
        <el-input v-model="ruleForm.name" />
      </el-form-item>
      <el-form-item label="所属项目:" prop="name">
        <el-input v-model="ruleForm.name" />
      </el-form-item>
      <el-form-item label="病历模板:" prop="region">
        <el-select v-model="ruleForm.region" placeholder="请选择">
          <el-option label="区域一" value="shanghai" />
        </el-select>
      </el-form-item>
      <el-form-item label="病历内容:" prop="region">
        <div class="editor">
          <editor-menu-bar v-slot="{ commands, isActive }" :editor="editor">
            <div class="menubar">

              <el-button
                class="menubar__button"
                :class="{ 'is-active': isActive.bold() }"
                @click="commands.bold"
              >
                <icon name="bold" />
              </el-button>

              <el-button
                class="menubar__button"
                :class="{ 'is-active': isActive.italic() }"
                @click="commands.italic"
              >
                <icon name="italic" />
              </el-button>

              <el-button
                class="menubar__button"
                :class="{ 'is-active': isActive.strike() }"
                @click="commands.strike"
              >
                <icon name="strike" />
              </el-button>

              <el-button
                class="menubar__button"
                :class="{ 'is-active': isActive.underline() }"
                @click="commands.underline"
              >
                <icon name="underline" />
              </el-button>

              <el-button
                class="menubar__button"
                :class="{ 'is-active': isActive.code() }"
                @click="commands.code"
              >
                <icon name="code" />
              </el-button>

              <el-button
                class="menubar__button"
                :class="{ 'is-active': isActive.paragraph() }"
                @click="commands.paragraph"
              >
                <icon name="paragraph" />
              </el-button>

              <el-button
                class="menubar__button"
                :class="{ 'is-active': isActive.heading({ level: 1 }) }"
                @click="commands.heading({ level: 1 })"
              >
                H1
              </el-button>

              <el-button
                class="menubar__button"
                :class="{ 'is-active': isActive.heading({ level: 2 }) }"
                @click="commands.heading({ level: 2 })"
              >
                H2
              </el-button>

              <el-button
                class="menubar__button"
                :class="{ 'is-active': isActive.heading({ level: 3 }) }"
                @click="commands.heading({ level: 3 })"
              >
                H3
              </el-button>

              <el-button
                class="menubar__button"
                :class="{ 'is-active': isActive.bullet_list() }"
                @click="commands.bullet_list"
              >
                <icon name="ul" />
              </el-button>

              <el-button
                class="menubar__button"
                :class="{ 'is-active': isActive.ordered_list() }"
                @click="commands.ordered_list"
              >
                <icon name="ol" />
              </el-button>

              <el-button
                class="menubar__button"
                :class="{ 'is-active': isActive.blockquote() }"
                @click="commands.blockquote"
              >
                <icon name="quote" />
              </el-button>

              <el-button
                class="menubar__button"
                :class="{ 'is-active': isActive.code_block() }"
                @click="commands.code_block"
              >
                <icon name="code" />
              </el-button>

              <el-button
                class="menubar__button"
                @click="commands.horizontal_rule"
              >
                <icon name="hr" />
              </el-button>

              <el-button
                class="menubar__button"
                @click="commands.undo"
              >
                <icon name="undo" />
              </el-button>

              <el-button
                class="menubar__button"
                @click="commands.redo"
              >
                <icon name="redo" />
              </el-button>

            </div>
          </editor-menu-bar>

          <editor-content class="editor__content" :editor="editor" />
        </div>
      </el-form-item>
      <el-form-item label="医生审核:" prop="resource">
        <el-radio-group v-model="ruleForm.resource">
          <el-radio label="通过" />
          <el-radio label="不通过" />
          <el-radio label="未审核" />
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-row class="twoButton">
          <el-col :span="3" :offset="5">
            <el-button type="primary" plain @click="submitForm('ruleForm')">提交</el-button>
          </el-col>
          <el-col :span="3" :offset="6">
            <el-button type="primary" plain>取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </Dialog>
</template>
<script>
import Icon from '@/components/Icon';
import { Editor, EditorContent, EditorMenuBar } from 'tiptap';
import {
  Blockquote,
  CodeBlock,
  HardBreak,
  Heading,
  HorizontalRule,
  OrderedList,
  BulletList,
  ListItem,
  TodoItem,
  TodoList,
  Bold,
  Code,
  Italic,
  Link,
  Strike,
  Underline,
  History,
} from 'tiptap-extensions';
import Dialog from '@/components/Dialog';
export default {
  components: {
    Dialog,
    EditorContent,
    EditorMenuBar,
    Icon,
  },
  data() {
    return {
      title: '写病历',
      visible: false,
      ruleForm: {
        name: '',
        region: '',
        resource: '',
      },
      editor: new Editor({
        extensions: [
          new Blockquote(),
          new BulletList(),
          new CodeBlock(),
          new HardBreak(),
          new Heading({ levels: [1, 2, 3] }),
          new HorizontalRule(),
          new ListItem(),
          new OrderedList(),
          new TodoItem(),
          new TodoList(),
          new Link(),
          new Bold(),
          new Code(),
          new Italic(),
          new Strike(),
          new Underline(),
          new History(),
        ],
        content: `
          <h2>
            Hi there,
          </h2>
        `,
      }),
    };
  },
  beforeDestroy() {
    this.editor.destroy();
  },
  methods: {
    close() {
      this.visible = false;
    },
    eaditMedical(val) {
      if (val === 'add') {
        this.title = "写病历";
      } else {
        this.title = "修改病历";
      }
      this.visible = true;
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log(this.ruleForm);
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.buttons {
  el-button {
    width: 140px;
    height: 40px;
  }
}

</style>
